package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.YWGiftCardDAO;
import com.ecoolex.discount.sales.core.external.v2.pojo.GetGiftCardListResponse;
import com.ecoolex.discount.sales.entity.YWGiftCard;
import com.ecoolex.discount.sales.entity.YWGiftCardExample;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class YWGiftCardManager {


    final static Map<String, Integer> CURRENCY_MAP = new HashedMap();

    static {
        CURRENCY_MAP.put("CNY", 1);
        CURRENCY_MAP.put("HKD", 2);
        CURRENCY_MAP.put("JPY", 3);
        CURRENCY_MAP.put("USD", 4);
    }

    @Autowired
    private YWGiftCardDAO dao;

    public void save(GetGiftCardListResponse.GiftCard giftCard) {
        int cardId = giftCard.getCardId();
        String channelId = giftCard.getChannelId();
        YWGiftCardExample.Criteria criteria = YWGiftCardExample.newAndCreateCriteria().andChannelIdEqualTo(channelId).andCardIdEqualTo(cardId);
        YWGiftCard ywGiftCard = dao.selectOneByExample(criteria.example());
        if (Objects.nonNull(ywGiftCard)) {
            BeanUtils.copyProperties(giftCard, ywGiftCard);
            if (0 < giftCard.getUpdateTime()) {
                ywGiftCard.setLastModifiedTime(Date.from(Instant.ofEpochSecond(giftCard.getUpdateTime())));
            }
            ywGiftCard.setUpdateUser("system-admin");
            ywGiftCard.setUpdateTime(Date.from(Instant.now()));
            dao.updateByPrimaryKey(ywGiftCard);
        } else {
            ywGiftCard = new YWGiftCard();
            BeanUtils.copyProperties(giftCard, ywGiftCard);
            ywGiftCard.setCurrency(CURRENCY_MAP.getOrDefault(giftCard.getCurrency(), -99));
            if (0 < giftCard.getUpdateTime()) {
                ywGiftCard.setLastModifiedTime(Date.from(Instant.ofEpochSecond(giftCard.getUpdateTime())));
            }
            ywGiftCard.setDeleted(YWGiftCard.NOT_DELETED);
            ywGiftCard.setCreateTime(Date.from(Instant.now()));
            ywGiftCard.setCreateUser("system-admin");
            ywGiftCard.setUpdateUser("system-admin");
            ywGiftCard.setUpdateTime(Date.from(Instant.now()));
            dao.insert(ywGiftCard);

        }
    }

    public List<YWGiftCard> getAllGiftCards() {
        YWGiftCardExample.Criteria criteria = YWGiftCardExample.newAndCreateCriteria()
                .andDeletedEqualTo(YWGiftCard.NOT_DELETED);
        return dao.selectByExample(criteria.example());
    }

    public YWGiftCard getGiftCardByServiceIdAndChannelIdAndCardId(String serviceId, String channelId,int cardId) {
        YWGiftCardExample.Criteria criteria = YWGiftCardExample.newAndCreateCriteria() //
                .andServiceIdEqualTo(serviceId) //
                .andChannelIdEqualTo(channelId) //
                .andCardIdEqualTo(cardId) //
                .andDeletedEqualTo(YWGiftCard.NOT_DELETED);
        return dao.selectOneByExample(criteria.example());
    }

    YWGiftCard.Column[] columns = {YWGiftCard.Column.deleted, YWGiftCard.Column.status, YWGiftCard.Column.updateUser, YWGiftCard.Column.updateTime};

    /**
     * 将下线的礼品卡设置成禁用
     * @param cardIds
     * @param serviceId
     * @param channelId
     * @return
     */
    public Integer disableOfflineCard(List<Integer> cardIds, String serviceId, String channelId) {
        YWGiftCard ywGiftCard = new YWGiftCard();
        ywGiftCard.setDeleted(YWGiftCard.IS_DELETED);
        ywGiftCard.setStatus("offline");
        ywGiftCard.setUpdateUser("system-admin");
        ywGiftCard.setUpdateTime(Date.from(Instant.now()));
        YWGiftCardExample.Criteria criteria = YWGiftCardExample.newAndCreateCriteria()
                .andCardIdNotIn(cardIds).andServiceIdEqualTo(serviceId).andChannelIdEqualTo(channelId);

        return dao.updateByExampleSelective(ywGiftCard, criteria.example(), columns);
    }
}
